"""
密码工具函数
"""
import hashlib
import secrets
import string


def generate_password_hash(password):
    """生成密码哈希"""
    if not password:
        return None
    
    # 使用SHA256加密（简单实现，生产环境建议使用bcrypt）
    return hashlib.sha256(password.encode('utf-8')).hexdigest()


def check_password_hash(password_hash, password):
    """验证密码"""
    if not password_hash or not password:
        return False
    
    return password_hash == generate_password_hash(password)


def generate_random_password(length=8):
    """生成随机密码"""
    characters = string.ascii_letters + string.digits
    return ''.join(secrets.choice(characters) for _ in range(length))


def validate_password_strength(password):
    """验证密码强度"""
    if not password:
        return False, "密码不能为空"
    
    if len(password) < 6:
        return False, "密码长度不能少于6位"
    
    if len(password) > 50:
        return False, "密码长度不能超过50位"
    
    # 检查是否包含字母和数字
    has_letter = any(c.isalpha() for c in password)
    has_digit = any(c.isdigit() for c in password)
    
    if not (has_letter and has_digit):
        return False, "密码必须包含字母和数字"
    
    return True, "密码强度合格"
